/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {pinyin4js} from './pinyin4js' 
import expectData from './expectData'

const BASE_COUNT=100
let data = '第五回虎牢关三英战吕洛阳井孙坚吞玺且说董卓见关羽一刀斩了华雄，没办法，只好让吕布带了兵出来抵抗。吕布称得上是三国中的第一条好汉，他长得英俊高大，是一位美男子，使一把方天戟，武艺高强，兼有一匹千里马“赤兔”，人称“人中吕布，马中赤兔”。但是他的人品不怎么样，没有骨气，反复无常，见利忘义，缺乏原则。所以张飞骂他“三姓家奴”（他本姓吕，后来拜丁原作义父改姓丁，再拜董卓为义父姓了董），人人对他不齿。这次他领了三万军马来到虎牢关，斩了两员大将，伤了两员大将，气焰十分张狂。公孙瓒亲自去与他作战，斗不三合，就心慌意乱，拨转马头就跑。谁知道吕布骑的是赤兔千里马，风一般地追来，眼看要追上了。边上有人大喝“三姓家奴休走，燕人张翼德在这里！”挺了丈八蛇矛截住了他。众人看时才看清原来是刘备的三弟张飞。吕布这人本17领出色，一般将军不出十个回合，不是死便是伤，从未见能与他对上百十个回合的，这回骤然间遇上一个硬手，三个回合打下来就知道不是好惹的，便打起精神与张飞一来一往大打起来。一打打了个五十回合，还看不出自己能不能赢，吕布正焦急着，边上又杀出个红脸长须的人，挺了一把八十二斤重的青龙偃月刀，一刀重似一刀地帮助张飞。光张飞一个吕布还好对付，加上一个关羽时间一长便难说了；就在这时候，又一个提着双剑杀了过来。这人武艺虽然不强，可前两个已经够他受了，吕布心想：“今天怎么这样晦气，已有两个硬底子了还有人来帮忙，我再不走，少不得伤在他们手下！”见第三个上的刘备武艺尚弱，便一连两戟刺向刘备。刘备抵敌不住，被撞开一条路，吕布趁着这个机会，拍拍马，飞一般冲出，逃了回去。以上这一仗也就是后来人们津津乐道的“三英战吕布”。从此以后，董卓就心里害怕，带了皇帝，逃到长安去了。众军阀攻进洛阳，纷纷争夺起好处来。孙坚是代表江东的军阀，他带兵进入皇宫，夜间发现井上有五色毫光，令兵下井，抱出一个女人的尸身来：装束宫里人模样，脖子下挂一个锦囊。打开看时，锦囊内有朱红色小匣一只，用金锁锁着。打开锁，正是皇帝日常用的玉玺：方圆四寸，上镌五龙交纽；旁缺一角，以黄金镶之；上有篆文八个字：“受命于天，既寿永昌。”孙坚心想，得了玉玺便可做皇帝，老天保佑我得了它，看来我是18三国演义有做皇帝的份儿了，心中大喜。想不到他的部下有个士兵是军阀袁绍的同乡，偷偷去报告了袁绍。孙坚得了玉玺便想及早回家，以便招兵买马将来当皇帝，就借口有病去向军阀头子袁绍请假。袁绍笑嘻嘻说：“你得的是玉玺病吧？这玉玺是国家所有，怎么可以私自独吞？”孙坚急了，发誓说：“我若得了玉玺，将来让我死在刀剑之下！”袁绍让那个报告的士兵出来作证，孙坚大怒，拔剑要杀那人，袁绍部下也都拔出刀来，事情闹僵，孙坚就连夜带兵走了。从此，那些军阀的心也就慢慢散了'

/* interface：convertToPinyinString */
function convertToPinyinString_Test_01() { 
    let result = ''
    let startTime = Date.now()
    for (let index = 0; index < BASE_COUNT; index++) { 
        result = pinyin4js.convertToPinyinString(data, ',', pinyin4js.WITH_TONE_MARK) 
    }
    let endTime = Date.now() 
    let time = endTime - startTime
    print("pinyin4js_ConvertToPinyinString(WITH_TONE_MARK): " + time +" ms")
    areEqual(expectData.WITH_TONE_MARK,result)

    // print(result)   
} 
/* interface：convertToPinyinString */
function convertToPinyinString_Test_02() { 
    let result = ''
    let startTime = Date.now()
    for (let index = 0; index < BASE_COUNT; index++) { 
        result = pinyin4js.convertToPinyinString(data, ',', pinyin4js.WITHOUT_TONE) 
    }
    let endTime = Date.now() 
    let time = endTime - startTime
    print("pinyin4js_ConvertToPinyinString(WITHOUT_TONE): " + time +" ms")
    areEqual(expectData.WITHOUT_TONE,result)

    // print(result)   
}
/* interface：convertToPinyinString */
function convertToPinyinString_Test_03() { 
    let result = ''
    let startTime = Date.now()
    for (let index = 0; index < BASE_COUNT; index++) { 
        result = pinyin4js.convertToPinyinString(data, ',', pinyin4js.WITH_TONE_NUMBER) 
    }
    let endTime = Date.now() 
    let time = endTime - startTime
    print("pinyin4js_ConvertToPinyinString(WITH_TONE_NUMBER): " + time +" ms")
    areEqual(expectData.WITH_TONE_NUMBER,result)

    // print(result)   
}
/* interface：convertToPinyinString */
function convertToPinyinString_Test_04() { 
    let result = ''
    let startTime = Date.now()
    for (let index = 0; index < BASE_COUNT; index++) { 
        result = pinyin4js.convertToPinyinString(data, ',', pinyin4js.FIRST_LETTER) 
    }
    let endTime = Date.now() 
    let time = endTime - startTime
    print("pinyin4js_ConvertToPinyinString(FIRST_LETTER): " + time +" ms")
    areEqual(expectData.FIRST_LETTER,result)

    // print(result)   
} 

/* interface：getShortPinyin */
function getShortPinyin_Test() { 
    let result = ''
    let startTime = Date.now()
    for (let index = 0; index < BASE_COUNT; index++) { 
        result = pinyin4js.getShortPinyin(data)
    }
    let endTime = Date.now() 
    let time = endTime - startTime
    print("pinyin4js_GetShortPinyin: " + time +" ms")
    areEqual(expectData.ShortPinyin,result)  

    // print(result)   
} 

/* Simplified to traditional */
function convertToTraditionalChinese_Test() { 
    let result = ''
    let startTime = Date.now()
    for (let index = 0; index < 1; index++) { 
        result = pinyin4js.convertToTraditionalChinese(data)
    }
    let endTime = Date.now() 
    let time = endTime - startTime
    print("pinyin4js_ConvertToTraditionalChinese: " + time +" ms")
    areEqual(expectData.ToTraditionalChinese,result)   

    // print(result)   
} 

/*  Traditional to simplified */
function convertToSimplifiedChinese_Test() {  
    let testData = expectData.ToTraditionalChinese
    let result = ''
    let startTime = Date.now()
    for (let index = 0; index < BASE_COUNT; index++) { 
        result = pinyin4js.convertToSimplifiedChinese(testData)
    }
    let endTime = Date.now() 
    let time = endTime - startTime
    print("pinyin4js_ConvertToSimplifiedChinese: " + time +" ms")
    areEqual(data,result)     

    // print(result)   
} 


 
/* Expected results and actual results verification  */
function areEqual(expected, actual) {
    // If expected and actual are both NaN, consider them equal.
    if (expected === actual || expected !== expected && actual !== actual) {
        // print('Success')
    } else {
        let error = 
        '  Failed areEqual test' +
        '  Expected: ' + expected +
        '  Actual:   ' + actual
        ;
        throw(error) 
    }
}

function test6() {
	let arr = new Array(10)
	let len = arr.length
	let start = Date.now()
	for (let i = 0; i < BASE_COUNT; i++) {
		arr[i % len] = pinyin4js.convertToPinyinString('厦门你好大厦厦门', '#', pinyin4js.WITHOUT_TONE)
	}
	let end = Date.now()
	print("pinyin4js_WITHOUT_TONE: " + (end - start)  + " ms")
	for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			print("test fail")
		}
	}
}

/*  Benchmark  */
function pinyin4jsRun() { 
    convertToPinyinString_Test_01()
    convertToPinyinString_Test_02()
    convertToPinyinString_Test_03()
    convertToPinyinString_Test_04() 

    getShortPinyin_Test() 

    convertToTraditionalChinese_Test()  

    convertToSimplifiedChinese_Test()   

    test6()
} 
pinyin4jsRun()